import Vue from 'vue'
import lodash from 'lodash'
// import { strikethrough } from 'ansi-colors'

Vue.filter('formatTime', function (value) {
  // console.log('formatTime', value)
  let date = new Date(value)
  let year = date.getFullYear()
  let month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : date.getMonth()
  let getDate = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  let hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  let minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  let second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  return `${year}-${month}-${getDate} ${hour}:${minute}:${second}`
})

Vue.filter('time', function (value) {
  if (!value) return ''
  let date = new Date(value)
  let year = date.getFullYear()
  let month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  let getDate = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  return `${year}-${month}-${getDate}`
}) // permission control

export function categoryFilter (category) {
  let option = {}
  if (lodash.isArray(category.subdivision)) {
    let children = category.subdivision.map(categoryFilter)
    option = {
      value: category.id,
      label: category.categoryType === 1 ? `${category.nameCn}/${category.nameEn}` : `${category.nameCn}`,
      children: children
    }
    return option
  } else {
    option = {
      value: category.id,
      label: category.categoryType === 1 ? `${category.nameCn}/${category.nameEn}` : `${category.nameCn}`
    }
  }
  return option
}

Vue.filter('statusFilter', function (status) {
  if (status === 0) return '待支付'
  else if (status === 1) return '待发货'
  else if (status === 2) return '待收货'
  else if (status === 3) return '交易成功'
  else if (status === 4) return '交易关闭'
  else if (status === 5) return '退款中'
  else if (status === 6) return '退款成功'
  else if (status === 7) return '退款失败'
})

Vue.filter('divideFilter', (value) => {
  var float = parseFloat(value || 0)
  return (float / 100).toFixed(2)
})

export function divide (value) {
  var float = parseFloat(value || 0)
  return (float / 100).toFixed(2)
}

/*
*return 整数
*/
Vue.filter('multiplyFilter', (value) => {
  var float = parseFloat(value || 0)
  return (float * 100).toFixed(0)
})

export function multiply (value) {
  var float = parseFloat(value || 0)
  return (float * 100).toFixed(0)
}

/*
* 数组转字符串，逗号分割
*/
export function arrayToString (array, char) {
  if (!char) {
    char = ','
  }
  let str = ''
  array.forEach((value, index) => {
    if (index) {
      str += char
    }
    str += value.url
  })
  return str
}

/*
* 字符串转数组：图片
*/
export function stringToArray (str, char) {
  let array = str.split(char)
  array = array.map((item) => {
    return {
      url: item
    }
  })
  return array
}

Vue.filter('specFilter', (value) => {
  if (!value) {
    return
  }
  let specJson = JSON.parse(value)
  let spec = '规格：'
  let flag = 0
  for (let key in specJson) {
    if (flag) {
      spec += '，'
    }
    flag = 1
    spec += specJson[key]
  }
  return spec
})
